package com.A21.Adapter;

import java.util.ArrayList;
import java.util.Locale;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.A21.Object.MSTProduct;
import com.A21MobileSales.InvoiceHistory;
import com.A21MobileSales.R;
import com.A21MobileSales.SimpleWebviewPopUp;

public class ProductSwipeListAdapter extends BaseAdapter {
	ViewHolder holder;
	LayoutInflater inflater;
	ArrayList<MSTProduct> ProductList;
	ArrayList<MSTProduct> mOriginalValues;
	private Context context;

	public ProductSwipeListAdapter() {
		// TODO Auto-generated constructor stub
	}

	public ProductSwipeListAdapter(Context _context, ArrayList<MSTProduct> map) {
		Activity act = (Activity) _context;
		this.context = _context;
		this.ProductList = map;
		this.mOriginalValues = new ArrayList<MSTProduct>();
		this.mOriginalValues.addAll(ProductList);
		inflater = (LayoutInflater) act
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	public int getCount() {
		return ProductList.size();
	}

	public Object getItem(int arg0) {
		return ProductList.get(arg0);
	}

	public long getItemId(int arg0) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View vi = convertView;
		if (convertView == null) {
			vi = inflater.inflate(R.layout.row_swipe_productselection, null);
			holder = new ViewHolder();
			holder.tvProduct = (TextView) vi.findViewById(R.id.tvProduct);
			holder.tvPName = (TextView) vi.findViewById(R.id.tvPName);
			holder.btnWHQty = (LinearLayout) vi.findViewById(R.id.btnWHQty);
			holder.btnOnPO = (LinearLayout) vi.findViewById(R.id.btnOnPO);
			holder.btnOnSO = (LinearLayout) vi.findViewById(R.id.btnOnSO);
			holder.btnInvoice = (LinearLayout) vi.findViewById(R.id.btnInvoice);
			vi.setTag(holder);
		} else {
			holder = (ViewHolder) vi.getTag();
		}
		try {

			final MSTProduct prod = ProductList.get(position);
			holder.tvProduct.setText(prod.Product);

			if (prod.PName == null) {
				prod.PName = "";
			}
			// String strEncoded=new
			// String(prod.PName.getBytes(Charset.forName("iso-8859-1")),
			// Charset.forName("UTF-8"));

			// holder.tvPName.setText(strEncoded);
			holder.tvPName.setText(prod.PName);
			holder.btnWHQty.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					Intent intent = new Intent(context,
							SimpleWebviewPopUp.class);
					Bundle bundle = new Bundle();
					bundle.putString("title1", "Warehouse Stock");
					bundle.putString("title2", prod.PName);
					bundle.putString("function", "GetWHQ");
					bundle.putString("other", prod.Product);
					intent.putExtras(bundle);
					Activity activity = (Activity) context;
					activity.startActivity(intent);

				}
			});

			holder.btnOnPO.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					Intent intent = new Intent(context,
							SimpleWebviewPopUp.class);
					Bundle bundle = new Bundle();
					bundle.putString("title1", "Warehouse Stock");
					bundle.putString("title2", prod.PName);
					bundle.putString("function", "GetOnPO");
					bundle.putString("other", prod.Product);
					intent.putExtras(bundle);
					Activity activity = (Activity) context;
					activity.startActivity(intent);
				}
			});

			holder.btnOnSO.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					Intent intent = new Intent(context,
							SimpleWebviewPopUp.class);
					Bundle bundle = new Bundle();
					bundle.putString("title1", "Warehouse Stock");
					bundle.putString("title2", prod.PName);
					bundle.putString("function", "GetOnSOQty");
					bundle.putString("other", prod.Product);
					intent.putExtras(bundle);
					Activity activity = (Activity) context;
					activity.startActivity(intent);

				}
			});

			holder.btnInvoice.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					Intent intent = new Intent(context, InvoiceHistory.class);
					Bundle bundle = new Bundle();
					bundle.putString("title1", "PRODUCT INVOICE HISTORY");
					bundle.putString("title2", prod.PName);
					bundle.putString("function", "PI");
					bundle.putString("Other", prod.Product);
					intent.putExtras(bundle);
					Activity activity = (Activity) context;
					activity.startActivity(intent);

				}
			});

		} catch (Exception ex) {
			ex.printStackTrace();
		}

		return vi;
	}

	private class ViewHolder {
		TextView tvProduct;
		TextView tvPName;
		LinearLayout btnWHQty;
		LinearLayout btnOnPO;
		LinearLayout btnOnSO;
		LinearLayout btnInvoice;
	}

	public void filter(String charText) {
		charText = charText.toLowerCase(Locale.getDefault());
		ProductList.clear();
		if (charText.length() == 0) {
			ProductList.addAll(mOriginalValues);
		} else {
			for (MSTProduct pc : mOriginalValues) {
				final String Code = pc.Product;
				final String Name = pc.PName;
				if (Code.toLowerCase().contains(
						charText.toString().toLowerCase())
						|| Name.toLowerCase().contains(
								charText.toString().toLowerCase())) {
					ProductList.add(pc);
				}
			}
		}
		notifyDataSetChanged();
	}
}